<?php
if (!defined("__WD__")) exit("Wrong Connect");

class Kong extends __Kong{
	
	function __construct() {

		parent::__construct();

		$this->load->config("member", $this->config);

		$this->load->module( "mysql" , $this->db );
		$this->load->module( "input" , $this->input );

		$this->load->model( "admin/admin" , $this->admin );
		
		$this->data->title = $this->data->kong->name." - ".$this->data->title;

	}
	function doit($args) {
		
		// Action!
		if ($this->input->post_exists("action")) {
			// kong Class.		
			$this->load->kong("proc");
			$this->proc = new Kong_Proc();
			$this->proc->doit();
		}

		$stat = "list";
		if ( !empty($args) ) $stat = array_shift($args);
		
		$this->data->status = "popup"; // 기본 메뉴.
		
		if ($stat === "list") $this->viewList();
		else if ($stat === "new") $this->viewNew();
		else if ($stat === "configure") {
			$this->data->status = "configure";
			$this->viewConfigure();
		}
		else $this->viewModify( $stat );
		
	}

	function viewList() {
		
		$page = $this->input->get("page", 1);
		$len_line = 20;
		
		$len = $this->db->count("select count(*) from `wd_popup`");
		$rows = $this->db->fetch_assoc("select * from `wd_popup` order by `idx` desc limit ".($page-1)*$len_line.", {$len_line}");

		$now = time();
		$cnt = count($rows);
		for ($i = 0; $i < $cnt; $i++) {
			$rows[$i]['n'] = $len - $i - ($page-1)*$len_line;
			$rows[$i]['options'] = @unserialize($rows[$i]['options']);
			$rows[$i]['ing'] = ($rows[$i]['fromdate'] < $now && $rows[$i]['todate'] > $now);
		}
		unset($cnt);

		$pagination = $this->admin->makePagination($len, array(
			"page" => $page,
			"len_line" => $len_line
		));

		$this->data->rows = $rows;
		$this->data->pagination = $pagination;

		// -- 끝.
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/list");
		$this->load->kong("view/footer");
		$this->_footer();
	
	}

	function viewNew() {

		$this->data->admin->redactor = true;
		
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/new");
		$this->load->kong("view/footer");
		$this->_footer();
			
	}
	function viewModify( $idx ) {

		$this->data->admin->redactor = true;

		$idx = (int)$idx;
			
		$this->load->model( 'twitter/bootstrap_form', $this->form );
		$result = $this->db->result("select * from `wd_popup` where `idx`=?", array($idx));
		$result['options'] = @unserialize($result['options']);
				
		$result['groups'] = explode(",", $result['groups']);

		if ($result['options'] !== false)
			$this->data->options = implode(",", array_keys($result['options']));

		$this->data->result = $result;
		$this->data->grouplist = $this->db->fetch_assoc("select * from `wd_group`");
		$this->data->use_jumin	= $this->config['form']['jumin'];
		$this->data->use_phone	= $this->config['form']['phone'];
		
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/modify");
		$this->load->kong("view/footer");
		$this->_footer();
			
			
	}
	function viewConfigure() {
		$this->_header();
		$this->load->kong("view/header");
		//$this->load->kong("view/new");
		$this->load->kong("view/footer");
		$this->_footer();
	}
	
}